import { createRouter, createWebHashHistory } from 'vue-router'

//
import mylocal from './local.js'
//导入组件
import login from './components/login.vue'
import index from './components/index.vue'

//创建路由实例
const router = createRouter({
    //路由被激活时，显示的样式，可以自定义一个样式
    linkActiveClass: 'router-active',

    history: createWebHashHistory(),
    routes: [
        { path: '/', redirect: 'login' },
        { path: '/login', name: 'login', component: login },
        { path: '/index', name: 'index', component: index }
    ]
})
router.beforeEach((to, from, next) => {
    let isLogin = mylocal.getString("is_login");
    console.log(isLogin);
    if (to.name === 'index') {
        if (isLogin === '1') {
            next()
        } else {
            next({ name: 'login' })
        }
    } else { next() }
})

//懒加载的方式
//   const router = createRouter({
//     history: createWebHashHistory(),
//     routes: [
//       { path: '/home', component: () => import('./components/Home.vue') },
//       { path: '/about', component: () => import('./components/About.vue') },
//     ]
//   })


export default router;